<?php

//error_reporting(0);
error_reporting(E_ALL);

require_once(dirname(dirname(dirname(dirname(__FILE__)))) . '/appconf.php');

$local_config = array(
    'bookshelf_name' => 'HKTB',
	'version' => 20000,
);

/*
  ipad2011_device_push_token : the standard push log
  
  ( for newsstand )
  ipad2011_working_push      : re-generated every Saturday Morning
  ipad2011_device_push_token_log     : keep recent-push token, every time the device go online will log once, should keep 1-3 months
  ipad2011_device_push_token_summary : keep the accumulated and recent figure. update from ipad2011_device_push_token_log
  
  ===========================================
  
  on every saturday, we will get latest token from ipad2011_device_push_token_log (sandbox will not store here)
  
  - update ipad2011_device_push_token_summary
  - and create a list in 'ipad2011_working_push'
  
  =========================================== 
*/

class v2013_hktb_v0200_push_token extends CGIApplication{
	
	function setup(){
		$this->run_modes('add');
		$this->start_mode('add');

		$this->set_path_info_position(array('device' => 0, 
											'device_id' => 1, 
											'token' => 2
											));
		parent::setup();
		$this->mode_param(array('path_info' => 0, 'param' => 'action')); // for not to use first param as action
	}


	function action_add(){

		$device       = strtolower($this->get_param('p', 'device', ''));
		$device_id    = $this->get_param('p', 'device_id', '');
		$token        = $this->get_param('p', 'token', '');

		if ($device_id == '' || $token == '' ){
			if ($device_id == ''){
				return 'Invalid request (invalid device id)';
			}
			if ($token == ''){
				return 'Invalid request (invalid push_token)';
			}
			return 'Invalid request';
		}


		$dbh_w = $this->db_connect('write');

		$h_class_setting = array(
			'dbh' => $dbh_w,
			'mode' => 'sandbox'
		);

		if($device=='ios'){
		
			$apns_bookshelf = getModel('MAPI_APNS_Bookshelf_HKTB', $h_class_setting);

			$h_param = array(
				'app_version' => $this->param('version'),
				'device_id'  => $device_id,
				'token' =>  $token,
			);
			$apns_bookshelf->registerDevice($h_param);	
		
		}elseif($device=='android'){

			$gcm_bookshelf = getModel('MAPI_GCM_Bookshelf_HKTB', $h_class_setting);

			$h_param = array(
				'app_version' => $this->param('version'),
				'device_id'  => $device_id,
				'token' =>  $token,
			);
			$gcm_bookshelf->registerDevice($h_param);
		
		}

		return "success !"; // return is not important in push token registration
	}
  
}//end class

$webapp = new v2013_hktb_v0200_push_token(
  array('PARAMS' => array_merge($site_global_config, $local_config))
);
$webapp->run();
?>